<?php

namespace App\Entity;

use App\Internal\Product\Range;
use App\Models\ProductRange;

class ProductRangeEntity extends ABaseEntity
{
    /**
     * ProductRangeEntity constructor.
     * @param ProductRange $model
     */
    public function __construct(ProductRange $model)
    {
        $this->model = $model;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return (int)$this->model->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->model->id = $id;
    }

    /**
     * @return int
     */
    public function getProductId(): int
    {
        return (int)$this->model->product_id;
    }

    /**
     * @param int $product_id
     */
    public function setProductId(int $product_id): void
    {
        $this->model->product_id = $product_id;
    }

    /**
     * @return int
     */
    public function getAreaCode(): int
    {
        return (int)$this->model->area_code;
    }

    /**
     * @param int $area_code
     */
    public function setAreaCode(int $area_code): void
    {
        $this->model->area_code = $area_code;
    }

    /**
     * @return int
     */
    public function getAreaParent(): int
    {
        return (int)$this->model->area_parent;
    }

    /**
     * @param int $area_parent
     */
    public function setAreaParent(int $area_parent): void
    {
        $this->model->area_parent = $area_parent;
    }

    /**
     * @return int
     */
    public function getAreaLevel(): int
    {
        return (int)$this->model->area_level;
    }

    /**
     * @param int $area_level
     */
    public function setAreaLevel(int $area_level): void
    {
        $this->model->area_level = $area_level;
    }

    /**
     * @return string
     */
    public function getAreaSystem(): string
    {
        return (string)$this->model->area_system;
    }

    /**
     * @param string $area_system
     */
    public function setAreaSystem(string $area_system): void
    {
        $this->model->area_system = $area_system;
    }

    /**
     * @param int $cityId
     * @param int $productId
     * @param int $level
     * @return array|self[]
     */
    public static function findCityArea(int $cityId, int $productId, int $level = Range::LEVEL_ONE): array
    {
        $cities = ProductRange::query()->where('product_id', $productId)->where('area_level', $level)
            ->where('area_parent', $cityId)->get();
        if ($cities->isEmpty()) {
            return [];
        }
        $arr = [];
        foreach ($cities as $city) {
            $arr[] = new self($city);
        }
        return $arr;
    }
}
